<!--2019/5/22 23:37. by soft-->
<#assign title="宿舍管理" houses='this'>
<#include 'header.ftl'>

<div class="layui-card">
  <div class="layui-card-header">
    <div class="card-title">
      宿舍管理
    </div>
    <div class="header-tools">
      <button onclick="methods.add()" class="layui-btn-sm layui-btn layui-btn-normal layui-inline">
        <i class="layui-icon layui-icon-add-circle"></i>添加
      </button>
    </div>
  </div>
  <div class="layui-card-body">
    <table id="tables" lay-filter="tables"></table>
  </div>
</div>

<script type="text/html" id="houseBox">
  <div class="layer-dialog-from">
    <form class="layui-form ">
      <input type="hidden" name="id" value="{{d.id?d.id:''}}">
      <div class="layui-form-item">
        <label class="layui-form-label">楼号</label>
        <div class="layui-input-block">
          <input class="layui-input" name="houseNo" {{d.name?'readonly':''}} value="{{d.houseNo?d.houseNo:''}}"
          lay-verify="required" lay-verType="tips" placeholder="楼号"/>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">房间号</label>
        <div class="layui-input-block">
          <input class="layui-input" name="roomNo" {{d.name?'readonly':''}} value="{{d.roomNo?d.roomNo:''}}"
          lay-verify="required" lay-verType="tips" placeholder="房间号"/>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">房间状态</label>
        <div class="layui-input-block">
          <select name="status">
            <option value="HASPEOPLE" {{d.status=='有人'?'selected':''}}>有人</option>
            <option value="NOPEOPLE" {{d.status=='没人'?'selected':''}}>没人</option>
            <option value="DISABLED" {{d.status=='禁住'?'selected':''}}>禁住</option>
          </select>
        </div>
      </div>

      <div class="layui-form-item">
        <div class="layui-input-block">
          {{# if (d.id == null) { }}
          <button class="layui-btn layui-btn-fluid layui-btn-normal" lay-submit lay-filter="add">添加</button>
          {{# } else { }}
          <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="edit">保存</button>
          {{# } }}
        </div>
      </div>
    </form>
  </div>
</script>

<script>
  var methods, layerIndex;
  layui.use(['laytpl', 'table', 'layer', 'form', 'jquery'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var $ = layui.$;
    var laytpl = layui.laytpl;

    var $houseBox = $('#houseBox');

    table.render({
      elem: '#tables',
      method: 'post',
      url: '/admin/froom/page',
      page: true,
      cols: [[
        {title: '序号', type: 'numbers'},
        {title: '楼号', field: 'houseNo'},
        {title: '房间号', field: 'roomNo'},
        {title: '寝室长', field: 'name'},
        {title: '电话', field: 'tel'},
        {title: '联系QQ', field: 'qq'},
        {title: '状态', field: 'status'},
        {title: '登录时间', field: 'loginTime'},
        {
          title: '操作', templet: function (s) {
            var f = '<button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>';
            if (!isEmpty(s.name) || !isEmpty(s.loginTime)) {
              f = '<button class="layui-btn layui-btn-xs layui-btn-danger layui-disabled" disabled>删除</button>';
            }
            return '<button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>' + f;
          }, width: 115
        }
      ]],
      parseData: parseData
    }); // table

    methods = {
      del: function (nt) {
        layer.alert('确定删除房间【' + nt.houseNo + "#" + nt.roomNo + '】', {title: '确定提示', icon: 3}, function (index) {
          $.post('/admin/froom/del/' + nt.id, function (data) {
            layer.msg('删除成功!', {icon: 6, time: 1200})
            table.reload('tables')
          })
          layer.close(index)
        })
      },
      edit: function (h) {
        laytpl($houseBox.html()).render(h, function (html) {
          methods.dialog(html, h.name ? '修改房间状态' : '修改房间信息');
        })
      },
      add: function () {
        laytpl($houseBox.html()).render({id: null}, function (html) {
          methods.dialog(html);
        })
      },
      dialog: function (html, title) {
        layerIndex = layer.open({
          type: 1,
          area: ['450px'],
          title: title ? title : '添加楼层',
          content: html
        });
        form.render('select');
      }
    }

    // 事件
    table.on("tool(tables)", function (e) {
      methods[e.event](e.data, e.tr);
    });

    form.on('submit(add)', function (fo) {
      $.post('/admin/froom/add', fo.field, function (data) {
        if (data.success) {
          layer.msg('添加成功！', {icon: 6, time: 1200});
          layer.close(layerIndex);
          table.reload('tables')
        } else {
          layer.alert(data.msg, {title: '错误详情', icon: 2})
        }
      });
      return false;
    })

    form.on('submit(edit)', function (fo) {
      $.post('/admin/froom/change', fo.field, function (data) {
        layer.msg('修改成功！', {icon: 6, time: 1200});
        layer.close(layerIndex);
        table.reload('tables')
      });
      return false;
    })

  });
</script>

<#include 'footer.ftl'>